$(document).ready(function(){
    $("#upload-input").on("change", function(){
//    　　	var bs64 = base64(document.getElementById("upload-input"));
    });
    var userId=localStorage.getItem("userId");
    $(".addsubmit .Submit").click(function(){
    	if(userId==0){
    		addUsers();
    	}
    	else{
    		updateUsers(userId);
    	}
    });
    
    if(userId!=0){
    	$.ajax({
            type: "POST",
            url: "./GetUserInfo", 
            data: "userId="+userId,
            success: function(json){
            	var json =JSON.parse(json);
            	//{"msg": "success"}
            	if(json!=null && json.msg=="success"&&json.user!=null){
            		$(".addmsg .Email").val(json.user.Email);
            		$(".addmsg .FirstName").val(json.user.FirstName);
            		$(".addmsg .LastName").val(json.user.LastName);
            		if(json.user.Gender=="M"){
            			$(".addmsg .Gender").eq(0).attr("checked","checked");
            			$(".addmsg .Gender").eq(1).attr("checked","");
            		}
            		if(json.user.Gender=="F"){
            			$(".addmsg .Gender").eq(0).attr("checked","");
            			$(".addmsg .Gender").eq(1).attr("checked","checked");
            		}
            		$(".addmsg .DateOfBirth").val(json.user.DateOfBirth);
            		$(".addmsg .Phone").val(json.user.Phone);
            		$(".addmsg .Photo").attr("src",json.user.Photo);
            		$(".addmsg .Address").val(json.user.Address);
            		if(json.user.RoleId==1){
            			$(".addmsg .RoleId").eq(0).attr("checked","checked");
            			$(".addmsg .RoleId").eq(1).attr("checked","");
            		}
            		if(json.user.RoleId==2){
            			$(".addmsg .RoleId").eq(0).attr("checked","");
            			$(".addmsg .RoleId").eq(1).attr("checked","checked");
            		}
            		
            	}
            	if(json!=null && json.msg=="用户信息不存在"){
            		alert("用户信息不存在");
            	}
            } 
         });
    }
});

function base64(file) {
	//byte,就是将3个字节扩充为4个字节，字符串
    var reader = new FileReader();
    var pos = file.files[0].name.lastIndexOf(".");
    var type = file.files[0].name.substring(pos + 1);

    if (type.toLowerCase() != "png" && type.toLowerCase() != 'jpg' && type.toLowerCase() != 'jpeg' && type.toLowerCase() != 'gif' && type.toLowerCase() != 'bmp') {
        alert("格式错误，请上传'png、jpg、jpeg、bmp、gif'格式文件");
        return;
    }

    reader.onloadend = (
        function(e) {
            imgurl = e.target.result;
            if(imgurl.length>138000){
                alert("图片大小超过100KB！");
            }
            else{
                $("#upload").attr("src",imgurl);
            }
        }
    );
    // Read the file
    reader.readAsDataURL(file.files[0]);
    
}

function addUsers(){
	var Email = $(".addmsg .Email").val();
	var Password="";
	var FirstName=$(".addmsg .FirstName").val();
	var LastName=$(".addmsg .LastName").val();
	var Gender=$(".addmsg .Gender").val();
	var DateOfBirth=$(".addmsg .DateOfBirth").val();
	var Phone=$(".addmsg .Phone").val();
	var Photo=$(".addmsg .Photo").val();
	var Address=$(".addmsg .Address").val();
	var  RoleId=$(".addmsg .RoleId").val();
    $.ajax({
        type: "POST",
        url: "./AddUser", 
        data: "Email="+Email+"&FirstName="+FirstName+"&LastName="+LastName+
        "&Gender="+Gender+"&DateOfBirth="+DateOfBirth+
        "&Phone="+Phone+"&Photo="+Photo+
        "&Address="+Address+"&RoleId="+RoleId,
        success: function(json){
        	var json =JSON.parse(json);
        	//{"msg": "success"}
        	if(json!=null && json.msg=="success"){
        		localStorage.setItem("userId",0);
        		location.href="./userlist.html"
        	}
        	if(json!=null && json.msg=="邮箱重复"){
        		alert("邮箱重复");
        	}
        } 
     });
}

function updateUsers(userId){
	var Email = $(".addmsg .Email").val();
	var Password="";
	var FirstName=$(".addmsg .FirstName").val();
	var LastName=$(".addmsg .LastName").val();
	var Gender=$(".addmsg .Gender").val();
	var DateOfBirth=$(".addmsg .DateOfBirth").val();
	var Phone=$(".addmsg .Phone").val();
	var Photo=$(".addmsg .Photo").attr("src");
	var Address=$(".addmsg .Address").val();
	var  RoleId=$(".addmsg .RoleId").val();
    $.ajax({
        type: "POST",
        url: "./UpdateUser", 
        data: "Email="+Email+"&FirstName="+FirstName+"&LastName="+LastName+
        "&Gender="+Gender+"&DateOfBirth="+DateOfBirth+
        "&Phone="+Phone+"&Photo="+Photo+
        "&Address="+Address+"&RoleId="+RoleId+
        "&userId="+userId,
        success: function(json){
        	var json =JSON.parse(json);
        	//{"msg": "success"}
        	if(json!=null && json.msg=="success"){
        		localStorage.setItem("userId",0);
        		location.href="./userlist.html"
        	}
        	if(json!=null && json.msg=="邮箱重复"){
        		alert("邮箱重复");
        	}
        } 
     });
}

